<template>
  <view class="container">	
	 <view class="input" >
		  <text style="margin-right: 10px;">期号:</text>
		<text style="margin-right: 10px;text-decoration: underline;">{{lotteryNo}}期</text>
	 </view>
	  <view class="input" >
	  	 <text style="margin-right: 10px;">彩种:</text>
	  	 <uni-data-checkbox selectedColor="#742305;" v-model="type" :localdata="sex"></uni-data-checkbox>
	  </view>
	  <view class="input" >
	  	<text class="label">项目名称：</text>
	  	  <uni-data-select style="background-color: white;"
	  				v-model="projectType"
	  				:localdata="projects"
	  				label=""
	  			  ></uni-data-select>
	  </view>
	 <view class="input" >
		 <text class="label">输入号码:</text>
		 <uni-easyinput v-model="numbers"  placeholder="请输入号码" type="number"  style="width: 70%;"/>
	</view>
	<view class="input" >
	  <text class="label">注数范围：</text>
	    <uni-data-select style="background-color: white;"
			v-model="selectCount"
			:localdata="options"
			@change="selectLimit"
			label=""
		  ></uni-data-select>
	</view>
	  <view style="width: 100%;margin-left: 5%;margin-top:10px;">
	    <textarea v-model="result"  disabled style="height:300px;background-color: white; border-radius: 10px;"
	  	 ></textarea>
		  <text >{{count}}注</text>
	  </view>
    </scroll-view>
  <uni-row style="margin-top:20px;">
	  <uni-col :span="12">
	    <button class="submit-button" @click="optionResult">计算</button>  
	  </uni-col>  
	  <uni-col :span="12">
	    <button class="submit-button" @click="copy">复制</button>  
	  </uni-col>
  </uni-row> 
  </view>
</template>

<script>
export default {
  data() {
    return {
		type:'plw',
		selectCount:0,
		sex: [{
				text: '排3',
				value: 'plw'
			}, {
				text: '3d',
				value: "fcsd"
			}],
		count:0,
		numbers:'',
		lotteryNo:0,
		result:'',
		projectType:1,
		projects: [{value: 1, text: '和尾'},{value: 2, text: '胆码'},{value: 3, text: '百位'},{value: 4, text: '十位'},{value: 5, text: '个位'},{value: 6, text: '跨度'}],
		options: [{value: 100, text: '1-100'},{value: 200, text: '1-200'},{value: 300, text: '1-300'},{value: 400, text: '1-400'},{value: 500, text: '1-500'},
		{value: 600, text: '1-600'},{value: 700, text: '1-700'},{value: 800, text: '1-800'},{value: 900, text: '1-900'},{value: 1000, text: '1-1000'}], // 下拉框选项
		selectedIndex: 0, // 默认选中第一个
		arr : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
		aiRecordResult:[],
    };
  }, 
  computed: {
    selectedOption() {
      return this.options[this.selectedIndex]; // 当前选中的选项
    },
  },
  onLoad() {
	 //  uni.setNavigationBarTitle({
		// frontColor: '#000000',
	 //  	title: "Ai智能缩水"
	 //  });
	  this.getLastLotteryNo()
  },
  methods: {
	  selectLimit(limit){
		this.$myRequest({
			method: 'get',
			url: '/getAiRecordByType?type='+this.type+"&limit="+limit
		}).then((res) => {
			 if (res.flag) {
				 this.aiRecordResult = res.data
				}
			})
	  },
	  getLastLotteryNo(){ // 获取最新期号
	  		  this.$myRequest({
	  		  	method: 'get',
	  		  	url: '/getLastLotteryNo',
	  		  	data: {type:this.type}
	  		  }).then((res) => {
	  			  if(res.flag){
	  				   this.lotteryNo = Number(res.data)+1
	  			  }
	  		})
	  },
	 optionResult(){
		  let numberArray = this.numbers.split("")
		 if(numberArray.length==0){
		 	uni.showToast({
		 		title: '号码必须填写！'
		 	});
		 	return 
		 }
		 if(this.selectCount==0){
		 	uni.showToast({
		 		title: '范围不许选择！'
		 	});
		 	return 
		 }
		let myres = this.getResult()
		let arr2 = []
		if(this.aiRecordResult.length !=0){
			arr2 = myres.filter((value) =>  this.aiRecordResult.includes(value));
		}else{
			arr2 = myres
		}
		this.result =arr2.join(" ")
		this.count = arr2.length
		//this.saveRecord(myres)
	 },
	 saveRecord(result){
			let numberArray = this.numbers.split("").map(Number);
	 		let data = {};
	 		const str = JSON.stringify(numberArray)
	 		const deviceId = uni.getStorageSync("phone");
	 		data.type = this.type; // 彩票类型
	 		data.optionType = this.optionType //按钮的类型编号 比如 组排 的 0 百位 1
	 		data.optionName =  this.optionName //按钮的类型名称  比如 组排 百位
	 		data.deviceId = deviceId; // 设备唯一编号 可以是手机号
	 		data.record = str;
	 		data.projectName = "Ai推荐"
	 		data.result = JSON.stringify(result)
	 		// 0 代表从走势图查看的 并且需要在个人记录中查看
	 		data.flag=3
	 		this.$myRequest({
	 			method: 'post',
	 			url: '/saveRecord',
	 			data: data
	 		}).then((res) => {
	 		 
	 		});
	 },
	 getResult(){
		  let numberArray = this.numbers.split("").map(Number);
		  let result = []
		 switch(this.projectType){
			 case 1: // 和尾
			 this.optionType = 43
			 this.optionName = "和尾"
			  result = this.getLotteryHTPAndTails(numberArray)
			 break
			 case 2: //胆码
			 this.optionType = 48
			 this.optionName = "胆码"
			 let a1  = this.generateCombinationsForOneArray(numberArray, this.arr, this.arr);
			 let a2 = this.generateCombinationsForOneArray(this.arr, numberArray, this.arr);  
			 let a3 = this.generateCombinationsForOneArray(this.arr, this.arr, numberArray);  
			 result = [...a1,...a2,...a3]
			 break
			 case 3: // 百位
			 this.optionType = 4
			 this.optionName = "百位"
			  result = this.generateCombinationsForOneArray(numberArray, this.arr, this.arr);
			 break
			 case 4: // 十位
			 this.optionType = 5
			 this.optionName = "十位"
			 result = this.generateCombinationsForOneArray(this.arr, numberArray, this.arr);
			 break
			 case 5: // 个位
			 this.optionType = 6
			 this.optionName = "个位"
			 result = this.generateCombinationsForOneArray(this.arr, this.arr, numberArray);
			 break
			 case 6: // 跨度
			 this.optionType = 38
			 this.optionName = "小大差"
			 result = this.getDifferenceSmallMiddleBig(numberArray);
			 break
		 }
		 // 排序数组
		 result.sort((a, b) => a - b);
		 // 去重
		 result = [...new Set(result)];
		 return result
	 },
	 getDifferenceSmallMiddleBig(numAarr){ // 小大差
	 		  let arr  = this.generateCombinationsForOneArray(this.arr,this.arr, this.arr);
	 		  arr = arr.filter(item =>{
					let spnum = item.split("").sort((a, b) => a - b);
	 				let g = Math.abs(parseInt(spnum[0]) -parseInt(spnum[2]))
	 				 
	 				return numAarr.includes(g)
	 		  })
	 		  return arr
	 },
	 getLotteryHTPAndTails(numAarr){ // 和尾
	   let arr =this.generateCombinationsForOneArray(this.arr,this.arr,this.arr)
	 		let result =  arr.filter(item =>{
	 		  	 				let spnum = item.split("")
	 		  	 				let h = Math.abs(parseInt(spnum[0]) + parseInt(spnum[1]) + parseInt(spnum[2]))%10
	 		  	 				 return numAarr.includes(h)
	 		  })
			  return result
	 },
	 generateCombinationsForOneArray(arr1, arr2, arr3) {
	   return arr1.flatMap(i =>   
	     arr2.flatMap(j =>   
	       arr3.map(k => `${i}${j}${k}`)  
	     )  
	   );  
	 },
	  copy(){
		  const textToCopy = this.result;
		  // 复制功能
		  uni.setClipboardData({
		  	data: textToCopy, // 要复制到剪贴板的文本
		  	success: () => {
		  		// 成功回调函数
		  		uni.showToast({
		  			title: '复制成功',
		  			icon: 'success'
		  		});
		  	},
		  	fail: () => {
		  		// 失败回调函数
		  		uni.showToast({
		  			title: '复制失败',
		  			icon: 'none'
		  		});
		  	}
		  });
	  }
	}
  }
</script>

<style>
.container {
  height: 100vh;
  padding: 10px;  
  background-color: #f6eee8;  
  border-radius: 10px;  
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); 
}

 .input{
	   display: flex; /* 使用 Flexbox 布局 */
	   align-items: center; /* 垂直居中对齐 */
	   margin-bottom: 20px;
	   width: 60%;
	   margin-left: 5%;
	   margin-top:10px;
 }

.picker {
  position: relative;
  border: none;
  outline: none;
 
  margin-top: 5px;
  width: 100%; /* 确保下拉框宽度 */
  background-color: transparent; /* 背景透明 */
  cursor: pointer; /* 鼠标指针样式 */
  font-size: 16px; /* 字体大小 */
   text-decoration: underline; /* 添加下划线 */
}
 

.submit-button {  
  width: 70%;  
  height: 45px;  
  background-color: #742305;  
  color: white;  
  border: none;  
  border-radius: 5px;  
  font-size: 16px;  
  cursor: pointer;  
  transition: background-color 0.3s;  
}  
	
 
 
 
</style>
